home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / irix / tools / gettime.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  5.3 KB  |  227 lines

  1. /*----------------------------------------------------------------------
  2.  * $Id: gettime.c,v 1.8 1993/01/22 10:52:55 carlson Exp $
  3.  *
  4.  * gettime.c    Gets the last modification time of the file and
  5.  *        writes it to stdout.
  6.  *
  7.  * Synopsis:
  8.  *    gettime [-d] [-i] [-c | -m | -a] [[+<format>] <file>] [[+<format>] \
  9.  *        <file>] ...
  10.  *
  11.  * Description:
  12.  *    This routine writes to stdout the file time for the file(s)
  13.  *    specified.  If -c is specified, the creation time is written;
  14.  *    -m for modification time and -a for access time.  Modification
  15.  *    time is assumed.  If format is specified, it will display the
  16.  *    time for all the following files (until another format is
  17.  *    specified) in the format given.  Otherwise, the date and time
  18.  *    will be printed according to the default of ctime(3C).  Format
  19.  *    is defined the same way as for the date(1) command.
  20.  *
  21.  *    The -d option enables debug printout.
  22.  *
  23.  *    The -i option overrides any format(s) provided and prints
  24.  *    the date in integer format.
  25.  *
  26.  * Revision history:
  27.  *    $Log: gettime.c,v $
  28.  * Revision 1.8  1993/01/22  10:52:55  carlson
  29.  * Just changed the header line to use RCS Id, instead of Header.
  30.  *
  31.  * Revision 1.7  91/11/22  15:02:53  carlson
  32.  * Get time printed properly, instead of the address.
  33.  * 
  34.  * Revision 1.6  91/07/28  00:36:26  carlson
  35.  * Allow a list of files to be processed.
  36.  * 
  37.  * Revision 1.5  91/07/22  13:33:05  carlson
  38.  * Modified to allow multiple files on the command line.
  39.  * 
  40.  * Revision 1.4  91/07/19  14:18:18  carlson
  41.  * Added Header for RCS.
  42.  * 
  43.  * Revision 1.3  91/07/19  13:51:15  carlson
  44.  * Added -i option which prints in the time in decimal.
  45.  * 
  46.  * Revision 1.2  91/07/19  13:25:20  carlson
  47.  * Modified to use new format for cftime which requires % symbols.
  48.  * 
  49.  * Revision 1.1  90/10/09  17:24:45  chris
  50.  * Initial revision
  51.  * 
  52.  * 12 Sep 1989  Christopher W. Carlson, Silicon Graphics Inc.
  53.  * Initial draft
  54.  *
  55.  *--------------------------------------------------------------------*/
  56.  
  57. #include <stdio.h>
  58. #include <sys/types.h>
  59. #include <sys/stat.h>
  60. #include <errno.h>
  61. #include <time.h>
  62. #include <ctype.h>
  63.  
  64. static struct    stat    f_stat;
  65. static struct    tm    tim_st;
  66. static char    cbuff[256], format[256] = "";
  67. static long    timdat;
  68. static enum    { CREATE, MODIFY, ACCESS } timetype = MODIFY;
  69. static enum    { FALSE, TRUE } debug, intfmt, one;
  70.  
  71. main (argc, argv)
  72.   int argc;
  73.   char *argv[];
  74. {
  75.     register int i, j;
  76.     register char *cpos;
  77.  
  78. /*----
  79.  * Initialize some variables.
  80.  *----*/
  81.  
  82.     debug = FALSE;            /* Assume no debug */
  83.     intfmt = FALSE;            /* Assume not integer format */
  84.     one = TRUE;                /* Assume only one file to do */
  85.  
  86. /*----
  87.  * Pick up the parameters.
  88.  *----*/
  89.  
  90.     for (j = 1; j < argc; j++) {
  91.  
  92.     /*----
  93.      * If the parameter starts with a '-', it is an option.  If not,
  94.      * it is the start of the +<format> <file> arguments.
  95.      *----*/
  96.  
  97.     if (*argv[j] == '-') {
  98.         switch (argv[j][1]) {
  99.           case 'a':
  100.         timetype = ACCESS;
  101.         break;
  102.  
  103.           case 'c':
  104.         timetype = CREATE;
  105.         break;
  106.  
  107.           case 'd':
  108.         debug = TRUE;
  109.         break;
  110.  
  111.           case 'i':
  112.         intfmt = TRUE;
  113.         break;
  114.  
  115.           case 'm':
  116.         timetype = MODIFY;
  117.         break;
  118.  
  119.           default:
  120.         fprintf (stderr, "gettime: Unrecognized option %s\n", argv[j]);
  121.         break;
  122.         }
  123.     } else {
  124.         break;
  125.     }
  126.     }
  127.  
  128. /*----
  129.  * Now we are looping through the +<format> <file> arguments.
  130.  *----*/
  131.  
  132.     for (; j < argc; j++) {
  133.  
  134.     /*----
  135.      * If the parameter starts with '+', it is a format.
  136.      *----*/
  137.  
  138.     if (*argv[j] == '+') {
  139.         for (i = 0, cpos = &argv[j][1]; *cpos; cpos++) {
  140.         format[i++] = '%';
  141.         format[i++] = *cpos;
  142.         }
  143.         format[i] = '\0';
  144.  
  145.      /*----
  146.       * Otherwise, the parameter is a file name.  Handle the file.
  147.       *----*/
  148.  
  149.     } else {
  150.  
  151.     /*----
  152.      * Let's find out if more than one file is to be processed.
  153.      * If 'one' is TRUE, we currently believe we only have to
  154.      * process one file.  Let's check the next argument and if
  155.      * it exists, we can assume we are going to process more than
  156.      * one file and we can set 'one' to false.  Note, if the next
  157.      * argument begins with a '+' or '-', keep scanning down the
  158.      * arguments until we find a legitimate argument.
  159.      *----*/
  160.  
  161.         if (one) {
  162.         for (i = j + 1; i < argc; i++) {
  163.             if ((argv[i][0] != '+') && (argv[i][0] != '-')) {
  164.             one = FALSE;
  165.             break;
  166.             }
  167.         }
  168.         }
  169.  
  170.     /*----
  171.      * If we are processing more than one file, print the
  172.      * file name in front of the time.
  173.      *----*/
  174.  
  175.         if (! one)
  176.         printf ("%s: ", argv[j]);
  177.  
  178.     /*----
  179.      * Get the status of the file.
  180.      *----*/
  181.  
  182.         if (stat (argv[j], &f_stat) < 0) {
  183.         printf ("File status error %d\n", errno);
  184.         continue;
  185.         }
  186.  
  187.     /*----
  188.      * Depending on which time the user wants, put it into timdat.
  189.      *----*/
  190.  
  191.         switch (timetype) {
  192.           case CREATE:
  193.         timdat = f_stat.st_ctime;
  194.         break;
  195.  
  196.           case MODIFY:
  197.         timdat = f_stat.st_mtime;
  198.         break;
  199.  
  200.           case ACCESS:
  201.         timdat = f_stat.st_atime;
  202.         break;
  203.  
  204.           default:
  205.         fprintf (stderr, "gettime:  *** System error 1 ***\n");
  206.         exit (-1);
  207.         }
  208.  
  209.     /*----
  210.      * Print the time out.  If -i option provided, print it in decimal.
  211.      * If a format was provided, print using the format.  Otherwise, just
  212.      * print it in ctime format.
  213.      *----*/
  214.  
  215.         if (intfmt) {
  216.         printf ("%d\n", timdat);
  217.         } else if (format[0] == '\0') {
  218.         cpos = ctime (&timdat);
  219.         printf ("%s", cpos);
  220.         } else {
  221.         cftime (cbuff, format, &timdat);
  222.         printf ("%s\n", cbuff);
  223.         }
  224.     }
  225.     }
  226. }
  227.